package com.sc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sc.mapper.PosttypeMapper;
import com.sc.pojo.Post;
import com.sc.pojo.Posttype;
import com.sc.service.PosttypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class PosttypeServiceImpl implements PosttypeService {
    @Autowired
    PosttypeMapper mapper;
    @Override
    public List<Posttype> queryAll() {
        return mapper.queryAll();
    }

    @Override
    public PageInfo<Posttype> show(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Posttype> list=mapper.queryAll();
        PageInfo<Posttype> p=new PageInfo<>(list);
        return p;
    }

    @Override
    public int add(Posttype pt) {
        return mapper.insertSelective(pt);
    }

    @Override
    public Posttype queryOne(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public int update(Posttype pt) {
        return mapper.updateByPrimaryKeySelective(pt);
    }

    @Override
    public int delete(Integer id) {
        //重置该类型帖子的类型位null
        mapper.reSetPostType(id);
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteAll(Integer[] ids) {
        int i=0;
        for (Integer id:ids){
            //重置该类型帖子的类型位null
            mapper.reSetPostType(id);
            i+= mapper.deleteByPrimaryKey(id);
        }
        return i;
    }
}
